package com.sky.state;

// 电梯状态
public abstract class LiftState {

    Context context;

    public abstract void open();

    public abstract void close();

    public abstract void run();

    public abstract void stop();

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }
}

// 开门状态
class OpenState extends LiftState {
    @Override
    public void open() {
        System.out.println("电梯门打开了");
    }

    @Override
    public void close() {
        super.context.setLiftState(Context.CLOSE_STATE);
        super.context.close();
    }

    @Override
    public void run() {

        System.out.println("开门状态, 无法运行");
    }

    @Override
    public void stop() {

        System.out.println("开门状态, 无法运行");
    }
}

// 关门状态
class CloseState extends LiftState {
    @Override
    public void open() {
        super.context.setLiftState(Context.OPEN_STATE);
        super.context.open();
        System.out.println("电梯正在关门.....");
    }

    @Override
    public void close() {
        System.out.println("电梯门已经关闭了!");
    }

    @Override
    public void run() {
        super.context.setLiftState(Context.RUN_STATE);
        super.context.run();
        System.out.println("关门状态, 无法运行");
    }

    @Override
    public void stop() {
        super.context.setLiftState(Context.STOP_STATE);
        super.context.stop();
        System.out.println("电梯未运行, 无法停止");
    }
}

// 运行状态
class RunState extends LiftState {
    @Override
    public void open() {
        System.out.println("运行状态, 无法开门, 请先停止电梯.....");
    }

    @Override
    public void close() {
        System.out.println("关门状态, 无法运行, 请先停止电梯.....");
    }

    @Override
    public void run() {
        System.out.println("电梯已经正在运行中.......");
    }

    @Override
    public void stop() {
        super.context.setLiftState(Context.STOP_STATE);
        super.context.stop();
        System.out.println("好的, 电梯正在停止....");
    }
}

// 停止状态
class StopState extends LiftState {
    @Override
    public void open() {
        super.context.setLiftState(Context.OPEN_STATE);
        super.context.open();
        System.out.println("停止状态, 无法开门, 请先运行电梯.....");
    }

    @Override
    public void close() {
        super.context.setLiftState(Context.CLOSE_STATE);
        super.context.close();
        System.out.println("停止状态, 无法关门, 请先运行电梯.....");
    }

    @Override
    public void run() {
        super.context.setLiftState(Context.RUN_STATE);
        super.context.run();
    }

    @Override
    public void stop() {
        System.out.println("好的,  电梯停止了!");
    }
}

/**  上下文  , 标记电梯运行状态  */
class Context {
    private LiftState liftState;
    static final LiftState OPEN_STATE = new OpenState();
    static final LiftState CLOSE_STATE = new CloseState();
    static final LiftState RUN_STATE = new RunState();
    static final LiftState STOP_STATE = new StopState();

    void setLiftState(LiftState liftState) {
        this.liftState = liftState;
        this.liftState.setContext(this);
    }

    void open() {
        this.liftState.open();
    }

    void close() {
        this.liftState.close();
    }

    void run() {
        this.liftState.run();
    }

    void stop() {
        this.liftState.stop();
    }
}

